package com.cloud.job.snail;


import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.api.domain.job.VehicleMileage;
import com.cloud.api.domain.job.VehicleRunningData;
import com.cloud.job.doris.DorisStreamLoad;
import com.cloud.job.service.IVehicleMessageDailyService;
import com.cloud.job.service.IVehicleMileageService;
import com.cloud.job.service.IVehicleRunningDataService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 杨斌
 * 2024-03-4/3/2024
 */
@Component
public class ExecuteVinRunDate {
    @Resource
    private IVehicleMessageDailyService vehicleMessageDailyService;
    @Resource
    private IVehicleRunningDataService vehicleRunningDataService;
    @Resource
    private IVehicleMileageService vehicleMileageService;
    @Resource
    private DorisStreamLoad dorisStreamLoad;
    // 统计车辆每日运行数据
    @JobExecutor(name = "vehicle_running_data")
    public void getVehicleRunningData() throws Exception {
        List<VehicleRunningData> vehicleRunningData = vehicleMessageDailyService.getVehicleRunningData();
        String jsonString = JSON.toJSONString(vehicleRunningData);
        dorisStreamLoad.loadJson(jsonString, VehicleRunningData.class);
    }
    @JobExecutor(name = "vehicle_mileages")
    // 统计车辆总运行历程，总运行时间，总充电量
    public void getVehicleMileages() throws Exception {
        List<VehicleMileage> vehicleMileages = vehicleRunningDataService.getVehicleMileages();
        String jsonString = JSON.toJSONString(vehicleMileages);
        dorisStreamLoad.loadJson(jsonString, VehicleMileage.class);
    }
//    @JobExecutor(name = "vehicle_mileages")
    public void getVehicleMileage() throws Exception {
        int pageSize = 100; // 每页的数据量
        int pageNum = 1; // 当前页数
        boolean hasMoreData = true;

        // 第一次查询，获取总记录数
        Page<VehicleMileage> firstPage = new Page<>(pageNum, pageSize);
        IPage<VehicleMileage> firstPageResult = vehicleRunningDataService.getVehicleMileages(firstPage);
        long totalRecords = firstPageResult.getTotal();

        if (totalRecords > 0) {
            // 计算总页数
            int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

            do {
                Page<VehicleMileage> page = new Page<>(pageNum, pageSize);
                IPage<VehicleMileage> pageResult = vehicleRunningDataService.getVehicleMileages(page);

                if (pageResult != null && pageResult.getRecords() != null && !pageResult.getRecords().isEmpty()) {
                    String jsonString = JSON.toJSONString(pageResult.getRecords());
                    dorisStreamLoad.loadJson(jsonString, VehicleMileage.class);
                    pageNum++;
                } else {
                    hasMoreData = false;
                }
            } while (pageNum <= totalPages && hasMoreData);
        }
    }
}
